home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1998 May / Macworld (1998-05).dmg / Serious Demos / Lasso 2.5 Test Drive / Lasso 2.5 CGI / Java / LassoProxy / LassoProxy.java < prev    next >
Text File  |  1997-12-12  |  6KB  |  212 lines

  1. /*
  2.     LassoProxy.java
  3.  
  4.     Communicates with Lasso running on the web server to process FileMaker Pro requests.
  5.     
  6.     Copyright © 1996 Blue World Communications, Inc.. All rights reserved.
  7. */
  8. package blueworld.lasso;
  9.  
  10. import java.util.*;
  11. import java.net.*;
  12. import java.io.*;
  13. import java.awt.Image;
  14. import java.awt.Toolkit;
  15. import java.awt.image.ImageProducer;
  16.  
  17. public class LassoProxy
  18. {
  19.     private    URL        fBaseURL;
  20.     private    String    fLasso;
  21.     boolean     debug = true;
  22.     
  23.     public LassoProxy( URL baseURL, String lasso )
  24.     {
  25.         fBaseURL = baseURL;
  26.         fLasso = new String(lasso);
  27.     }
  28.     
  29.     public LassoProxy(URL base)
  30.     {    this(base, "action,lasso");    }
  31.     
  32.     static BitSet dontNeedEncoding;
  33.  
  34.     /* The list of characters that are not encoded have been determined by 
  35.        referencing O'Reilly's "HTML: The Definitive Guide" (page 164). */
  36.        
  37.     static {
  38.     dontNeedEncoding = new BitSet(256);
  39.     int i;
  40.     for (i = 'a'; i <= 'z'; i++) {
  41.         dontNeedEncoding.set(i);
  42.     }
  43.     for (i = 'A'; i <= 'Z'; i++) {
  44.         dontNeedEncoding.set(i);
  45.     }
  46.     for (i = '0'; i <= '9'; i++) {
  47.         dontNeedEncoding.set(i);
  48.     }
  49.     dontNeedEncoding.set(' '); /* encoding a space to a + is done in the encode() method */
  50.     dontNeedEncoding.set('-');
  51.     dontNeedEncoding.set('_');
  52.     dontNeedEncoding.set('.');
  53.     dontNeedEncoding.set('*');
  54.     }
  55.     public static String URLencodeForUTF(String s) {
  56.     int maxBytesPerChar = 10;
  57.     ByteArrayOutputStream out = new ByteArrayOutputStream(s.length());
  58.  
  59.     for (int i = 0; i < s.length(); i++) {
  60.         int c = (int)s.charAt(i);
  61.         if (dontNeedEncoding.get(c)) {
  62.         if (c == ' ') {
  63.             c = '+';
  64.         }
  65.         out.write(c);
  66.         } else {
  67.             out.write('%');
  68.             out.write(Character.forDigit((c >> 4) & 0xF, 16));
  69.             out.write(Character.forDigit(c & 0xF, 16));
  70.         }
  71.     }
  72.  
  73.     return out.toString();
  74.     }
  75.     // First UTF-8 encodes, then URL encodes the input string
  76.     public static String prepare(String in) throws IOException
  77.     {    return URLencodeForUTF(UTF8Coder.encode(in));    }
  78.     
  79.     public String[] databaseNames() throws IOException
  80.     {
  81.         return getNames( new URL( fBaseURL, fLasso + "?[dbnames]" ) );
  82.     }
  83.     
  84.     public String[] layoutNames( String databaseName ) throws IOException
  85.     {
  86.         return getNames( new URL( fBaseURL, fLasso + "?[database]=" +
  87.                                              LassoProxy.prepare(databaseName) +
  88.                                              "&[layoutnames]" ) );
  89.     }
  90.     // request contains the recid, databasename and layout name.
  91.     // fieldName is the name of the field which contains the image
  92.     // bitsPerPixel can be either 16 or 32
  93.     // imageQuality can be from 0 to 4
  94.     public Image getJPEG(LassoRequest request, String fieldName, int bitsPerPixel, int imageQuality)
  95.         throws MalformedURLException, IOException
  96.     {
  97.         // must have set a recid for this to work
  98.         if (request.recordID() < 0)
  99.             return null;
  100.         if (bitsPerPixel > 32) bitsPerPixel = 32;
  101.         if (bitsPerPixel < 16) bitsPerPixel = 16;
  102.         if (imageQuality < 0) imageQuality = 0;
  103.         if (imageQuality > 4) imageQuality = 4;        
  104.         
  105.         StringBuffer    requestString =  makeImageString(request);
  106.  
  107.         requestString.append("&").append(LassoProxy.prepare(fieldName)).append("=jpeg,").append(bitsPerPixel);
  108.         requestString.append(",").append(imageQuality);
  109.  
  110.         return Toolkit.getDefaultToolkit().createImage((ImageProducer)(new URL(fBaseURL, requestString.toString())).getContent());
  111.     }
  112.     
  113.     // request contains the recid, databasename and layout name.
  114.     // fieldName is the name of the field which contains the image
  115.     // bitsPerPixel can be either 1, 2, 4 or 8
  116.     public Image getGIF(LassoRequest request, String fieldName, int bitsPerPixel, boolean interlace)
  117.         throws MalformedURLException, IOException
  118.     {
  119.         // must have set a recid for this to work
  120.         if (request.recordID() < 0)
  121.             return null;
  122.         if (bitsPerPixel > 8) bitsPerPixel = 8;
  123.         if (bitsPerPixel < 1) bitsPerPixel = 1;    
  124.         
  125.         StringBuffer requestString = makeImageString(request);
  126.  
  127.         requestString.append("&").append(LassoProxy.prepare(fieldName)).append("=gif,");
  128.         requestString.append(bitsPerPixel).append(",").append((interlace) ? "true" : "false");
  129.         URL url = new URL(fBaseURL, requestString.toString());
  130.         return Toolkit.getDefaultToolkit().createImage((ImageProducer)(new URL(fBaseURL, requestString.toString())).getContent());
  131.     }
  132.     
  133.     protected StringBuffer makeImageString(LassoRequest req) throws IOException
  134.     {
  135.         StringBuffer    requestString = new StringBuffer(getLasso());
  136.         requestString.append("?[image]&[database]=").append(LassoProxy.prepare(req.databaseName()));
  137.         if (req.layoutName().length() > 0)
  138.             requestString.append("&[layout]=").append(LassoProxy.prepare(req.layoutName()));
  139.         requestString.append("&[recid]=").append(req.recordID());
  140.         return requestString;
  141.     }
  142.     
  143.     // return the current Lasso name
  144.     public String    getLasso()
  145.     {    return fLasso;    }
  146.     
  147.     public LassoResponse processRequest( LassoRequest request ) throws IOException
  148.     {
  149.         URL         requestURL = new URL(fBaseURL, request.toString());
  150. //        URL         requestURL = new String(request.toString());
  151.         if(debug == true)
  152.             System.out.println("request is: " + requestURL);
  153.         String     response   = sendHTTPRequest(requestURL);
  154.         if(debug == true)
  155.             System.out.println("response is: " + response);
  156.         return ( new LassoResponse( response ) );
  157.     }
  158.     
  159.     private String[] getNames( URL url ) throws IOException
  160.     {
  161.         String             response  = sendHTTPRequest( url );
  162.         StringTokenizer tokenizer = new StringTokenizer( response.toString(), " È\r\n" );
  163.         int             numNames  = tokenizer.countTokens();
  164.         String             names[]   = new String[ numNames ];
  165.         
  166.         for ( int i = 0; i < numNames; i++ )
  167.             names[ i ] = tokenizer.nextToken();
  168.         
  169.         return names;
  170.     }
  171.     
  172.     private String sendHTTPRequest( URL url ) throws IOException
  173.     {
  174.         URLConnection     connection = url.openConnection();
  175.         
  176.         connection.setUseCaches( false );
  177.         connection.setAllowUserInteraction( true );        // experimental
  178.         
  179.         int              inputChar;
  180.         InputStream    response = connection.getInputStream();        // send HTTP request
  181.         StringBuffer    buffer = new StringBuffer();
  182.         String        result = null;
  183.         
  184.         try
  185.         {    
  186.             try
  187.             {
  188.                 while ( (inputChar = response.read()) != -1)
  189.                     buffer.append((char)inputChar);
  190.             } catch(IOException e) {}
  191.             result = UTF8Coder.decode(buffer.toString());
  192.         } 
  193.         catch(IOException e)
  194.         {     System.err.println(e.toString()); result = new String();    }
  195.         
  196.         response.close();
  197.         
  198.         return result;
  199.     }
  200.     
  201.     public void setDebug(boolean state)
  202.     {
  203.         debug = state;
  204.     }
  205.     
  206.     public boolean getDebug()
  207.     {
  208.         return debug;
  209.     }
  210. }
  211.  
  212.